package edu.guigu.todolist.todo.presenter;

import java.util.List;

import edu.guigu.todolist.todo.model.TodoModel;
import edu.guigu.todolist.todo.model.TodoModelImpl;
import edu.guigu.todolist.todo.model.info.TodoInfo;
import edu.guigu.todolist.todo.view.TodoView;

public class TodoPresenterImpl implements TodoPresenter {

    private TodoView todoView;
    private TodoModel todoModel;

    public TodoPresenterImpl(TodoView todoView) {
        this.todoModel = TodoModelImpl.getInstance();
        this.todoView = todoView;
        this.todoView.initView();
    }

    @Override
    public void loadTodoList() {
        todoModel.requestTodoData(new TodoModel.Callback() {
            @Override
            public void onSuccess(List<TodoInfo> todoInfos, int position) {
                todoView.showTodoListView(todoInfos, TodoView.TYPE_QUEST_DATA, position);
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public void addTodo(TodoInfo todoInfo) {
        todoModel.addTodoData(todoInfo, new TodoModel.Callback() {
            @Override
            public void onSuccess(List<TodoInfo> todoInfos, int position) {
                todoView.showTodoListView(todoInfos, TodoView.TYPE_ADD_DATA, position);
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public void removeTodo(int position) {
        todoModel.delTodoData(position, new TodoModel.Callback() {
            @Override
            public void onSuccess(List<TodoInfo> todoInfos, int position) {
                todoView.showTodoListView(todoInfos, TodoView.TYPE_REMOVE_DATA, position);
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public void onDestroy() {
        todoView = null;
    }
}
